<?php
namespace Validator;

use Hoa\Registry\Registry;
use Library\Arr;
use Respect\Validation\Validator;
use Respect\Validation\Exceptions\NestedValidationException ;


/**
 * DictValidator
 * @author los_gsy
 */
class DictValidator {
	/**
	 * 验证编码
	 * @param string $code
	 * @return mixed true：表示验证通过；array：表示验证失败，该返回数组即为错误信息；
	 */
	static public function validateCode($code) {
		$conf = Registry::get('conf');
		$codeValidator = Validator::notEmpty()->alnum('_')->length(null, 64);
		try {
			$codeValidator->assert($code);
		} catch (NestedValidationException $e) {
			$eMsgs = $e->findMessages([
					'notEmpty' => 410401,
					'alnum' => 410402,
					'length' => 410403,
			]);
			$status = Arr::firstNotEmpty($eMsgs);
			if ($status) {
				$ret = $conf['status'][$status];
				return $ret;
			}
		}
		return true;
	}


	/**
	 * 验证名称
	 * @param string $name
	 * @return mixed true：表示验证通过；array：表示验证失败，该返回数组即为错误信息；
	 */
	static public function validateName($name) {
		$conf = Registry::get('conf');
		$nameValidator = Validator::notEmpty()->stringType()->length(null, 64);
		try {
			$nameValidator->assert($name);
		} catch (NestedValidationException $e) {
			$eMsgs = $e->findMessages([
					'notEmpty' => 410501,
					'alnum' => 410502,
					'length' => 410503,
			]);
			$status = Arr::firstNotEmpty($eMsgs);
			if ($status) {
				$ret = $conf['status'][$status];
				return $ret;
			}
		}
		return true;
	}


	/**
	 * 验证类型
	 * @param string $type
	 * @return mixed true：表示验证通过；array：表示验证失败，该返回数组即为错误信息；
	 */
	static public function validateType($type) {
		$conf = Registry::get('conf');
		$typeValidator = Validator::notEmpty()->stringType()->length(null, 64);
		try {
			$typeValidator->assert($type);
		} catch (NestedValidationException $e) {
			$eMsgs = $e->findMessages([
					'notEmpty' => 410601,
					'alnum' => 410602,
					'length' => 410603,
			]);
			$status = Arr::firstNotEmpty($eMsgs);
			if ($status) {
				$ret = $conf['status'][$status];
				return $ret;
			}
		}
		return true;
	}




}